package com.ztesoft.huangfu.learning.springboot.springbootssjwt.service.impl;

import com.ztesoft.huangfu.learning.springboot.springbootssjwt.dao.UserDao;
import com.ztesoft.huangfu.learning.springboot.springbootssjwt.entity.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import static java.util.Collections.emptyList;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/6/10
 * Time: 10:36
 * Version: 1.0
 * Description:
 **/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private UserDao userdao;

    /**
     * 通过构造器注入UserRepository
     *
     * @param userdao
     */
    public UserDetailsServiceImpl(UserDao userdao) {
        this.userdao = userdao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userdao.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), emptyList());
    }

}
